package ourfilter;


import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;

import ourImage.OurPlanarImage;
import pimpmypipe.filter.DataTransformationFilter;
import pimpmypipe.interfaces.Writeable;
import pimpmypipe.interfaces.Readable;
import pimpmypipe.pipe.Pipe;

/**
 * Opening Filter: Including erode and dilate
 * 
 * @author Meraner Marcel	
 */
public class ErodeFilter extends DataTransformationFilter<OurPlanarImage> {

    // Erode Matrix
    private float[] erode = new float[]{
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
        0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
        0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
        0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };
    
    private int _matrixSize = 9;
    private KernelJAI kernelErode = null;
    private boolean _filterEnabled = false;
    private float _matrixValue = 1.0f;
   
    public ErodeFilter(Pipe<OurPlanarImage> in, Pipe<OurPlanarImage> out) {
        super(in, out);
        in.setOut(this);
        out.setIn(this);
        // Generate Kernels
        
        kernelErode = new KernelJAI(_matrixSize, _matrixSize, erode);
    }
    public ErodeFilter(Readable<OurPlanarImage> in, Writeable<OurPlanarImage> out){
        super(in, out);
        kernelErode = new KernelJAI(_matrixSize, _matrixSize, erode);
        
    }

    @Override
    protected void process(OurPlanarImage entity) {
        if (_filterEnabled) {
            // Erode the given image
            ParameterBlock pbErode = new ParameterBlock();
            pbErode.addSource(entity.getImage());
            pbErode.add(kernelErode);
            PlanarImage image = JAI.create("erode", pbErode);
            entity.setImage(image);
        }
    }


    public void setMatrixSize(int matrixSize) {
        _matrixSize = matrixSize;
        kernelErode = new KernelJAI(_matrixSize, _matrixSize, erode);
    }

    public boolean isEnabled() {
        return _filterEnabled;
    }

    public void setFilterEnabled(boolean enabled) {
        _filterEnabled = enabled;
    }


    public void setMatrixValue(float matrixValue) {
        _matrixValue = matrixValue;
        kernelErode = new KernelJAI(_matrixSize, _matrixSize, erode);
    }
}
